import { Service } from '../../services/service'
import { formaturl, formatFilterId } from '@/utils/commonUtils'
import { Message } from 'element-ui'
const state = {
  currentTab: sessionStorage.getItem('currentTab') || '1',
  queryKey: '',
  total: '',
  listData: [],
  typeId: '',
  filterId: {},
  canShow: true,
  requestUrlMapping: {
    1: '/hanyun/app-service/api/v1/open/app/list',
    2: '/hanyun/app-service/api/v1/open/service/list',
    3: '/hanyun/app-service/api/v1/open/model/list'
  },
  filterReqIdMapping: {
    1: 1,
    2: 0,
    3: 2
  },
  filterUrl: '/hanyun/app-service/api/v1/open/type/query',
  filterData: [],
  pagnation: {
    pageSize: 8,
    pageNum: 1,
    total: 0
  }
}
const getters = {}
const mutations = {
  setCurrenttab(state, tab) {
    state.currentTab = tab
  },
  setFilterId(state, data) {
    state.filterId = data
  },
  setCanShow(state, show) {
    state.canShow = show
  },
  setQuerykey(state, key) {
    state.queryKey = key
  },
  setTotal(state, total) {
    state.total = total
  },
  setTypeId(state, id = '') {
    if (state.typeId === id) {
      state.typeId = ''
    } else {
      state.typeId = id
    }
  },
  setListData(state, data) {
    state.listData = data
  },
  setPagnation(state, pagnation) {
    state.pagnation = Object.assign({}, state.pagnation, pagnation)
  },
  setFilterData(state, data) {
    data.forEach((item) => {
      item.label = item.name
      item.value = item.id
      if (state.currentTab === '1' || state.currentTab === '3') {
        item.leaf = true
      } else {
        item.leaf = item.parentId !== '-1'
      }
    })
    state.filterData = data
  }
}
const actions = {
  async getList({ commit, state }) {
    const currentTab = state.currentTab || '1'
    const data = await Service({
      url:
        state.requestUrlMapping[currentTab] +
        formaturl({
          typeId: state.typeId,
          name: state.queryKey,
          ...state.pagnation
        }, false) +
        formatFilterId(state.filterId), // todo
      method: 'get'
    })
    return new Promise((resolve, reject) => {
      if (data.status === 200) {
        data.result.data.forEach((item) => (item.des = item.description))
        commit('setListData', data.result.data)
        commit('setTotal', data.result.total)
        const { pageNum, pageSize, total } = data.result
        commit('setPagnation', {
          pageNum,
          pageSize,
          total: +total
        })
        resolve('ok')
      } else {
        Message({
          message: data.msg,
          type: 'error'
        })
      }
    })
  },
  resetStatus({ commit }) {
    commit('setCurrenttab', sessionStorage.getItem('currentTab') || '1')
    commit('setTypeId', '')
  },
  async getFilters({ commit, state }, params = {}) {
    const ret = await Service({
      url:
        state.filterUrl +
        formaturl({
          category: state.filterReqIdMapping[state.currentTab],
          ...params
        }, false),
      method: 'get'
    })
    return new Promise((resolve, reject) => {
      if (ret.status === 200) {
        commit('setFilterData', ret.result)
        resolve(ret.result)
      }
    })
  }
}
export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}
